home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / unlzw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  8.9 KB  |  396 lines  |  [TEXT/KAHL]

  1. /* unlzw.c -- decompress files in LZW format.
  2.  * The code in this file is directly derived from the public domain 'compress'
  3.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  4.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  5.  *
  6.  * This is a temporary version which will be rewritten in some future version
  7.  * to accommodate in-memory decompression.
  8.  */
  9.  
  10. /*
  11.  * Modified:1993 by SPDsoft for MacGzip
  12.  *
  13.  */
  14.  
  15. #ifdef RCSID
  16. static char rcsid[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $";
  17. #endif
  18.  
  19. #include <sys/types.h>
  20.  
  21. #include "tailor.h"
  22.  
  23. #ifdef HAVE_UNISTD_H
  24. #  include <unistd.h>
  25. #endif
  26. #ifndef NO_FCNTL_H
  27. #  include <fcntl.h>
  28. #endif
  29.  
  30. #include "gzip.h"
  31. #include "lzw.h"
  32.  
  33. #include "MacErrors.h"
  34. #include "SPDProg.h"
  35. #include <signal.h>
  36. extern long tell(int fn);
  37.  
  38. typedef    unsigned char    char_type;
  39. typedef          long   code_int;
  40. typedef unsigned long     count_int;
  41. typedef unsigned short    count_short;
  42. typedef unsigned long     cmp_code_int;
  43.  
  44. #define MAXCODE(n)    (1L << (n))
  45.     
  46. #ifndef    REGISTERS
  47. #    define    REGISTERS    2
  48. #endif
  49. #define    REG1    
  50. #define    REG2    
  51. #define    REG3    
  52. #define    REG4    
  53. #define    REG5    
  54. #define    REG6    
  55. #define    REG7    
  56. #define    REG8    
  57. #define    REG9    
  58. #define    REG10
  59. #define    REG11    
  60. #define    REG12    
  61. #define    REG13
  62. #define    REG14
  63. #define    REG15
  64. #define    REG16
  65. #if REGISTERS >= 1
  66. #    undef    REG1
  67. #    define    REG1    register
  68. #endif
  69. #if REGISTERS >= 2
  70. #    undef    REG2
  71. #    define    REG2    register
  72. #endif
  73. #if REGISTERS >= 3
  74. #    undef    REG3
  75. #    define    REG3    register
  76. #endif
  77. #if REGISTERS >= 4
  78. #    undef    REG4
  79. #    define    REG4    register
  80. #endif
  81. #if REGISTERS >= 5
  82. #    undef    REG5
  83. #    define    REG5    register
  84. #endif
  85. #if REGISTERS >= 6
  86. #    undef    REG6
  87. #    define    REG6    register
  88. #endif
  89. #if REGISTERS >= 7
  90. #    undef    REG7
  91. #    define    REG7    register
  92. #endif
  93. #if REGISTERS >= 8
  94. #    undef    REG8
  95. #    define    REG8    register
  96. #endif
  97. #if REGISTERS >= 9
  98. #    undef    REG9
  99. #    define    REG9    register
  100. #endif
  101. #if REGISTERS >= 10
  102. #    undef    REG10
  103. #    define    REG10    register
  104. #endif
  105. #if REGISTERS >= 11
  106. #    undef    REG11
  107. #    define    REG11    register
  108. #endif
  109. #if REGISTERS >= 12
  110. #    undef    REG12
  111. #    define    REG12    register
  112. #endif
  113. #if REGISTERS >= 13
  114. #    undef    REG13
  115. #    define    REG13    register
  116. #endif
  117. #if REGISTERS >= 14
  118. #    undef    REG14
  119. #    define    REG14    register
  120. #endif
  121. #if REGISTERS >= 15
  122. #    undef    REG15
  123. #    define    REG15    register
  124. #endif
  125. #if REGISTERS >= 16
  126. #    undef    REG16
  127. #    define    REG16    register
  128. #endif
  129.     
  130. #ifndef    BYTEORDER
  131. #    define    BYTEORDER    0000
  132. #endif
  133.     
  134. #ifndef    NOALLIGN
  135. #    define    NOALLIGN    0
  136. #endif
  137.  
  138.  
  139. union    bytes {
  140.     long  word;
  141.     struct {
  142. #if BYTEORDER == 4321
  143.     char_type    b1;
  144.     char_type    b2;
  145.     char_type    b3;
  146.     char_type    b4;
  147. #else
  148. #if BYTEORDER == 1234
  149.     char_type    b4;
  150.     char_type    b3;
  151.     char_type    b2;
  152.     char_type    b1;
  153. #else
  154. #    undef    BYTEORDER
  155.     int  dummy;
  156. #endif
  157. #endif
  158.     } bytes;
  159. };
  160.  
  161. #if BYTEORDER == 4321 && NOALLIGN == 1
  162. #  define input(b,o,c,n,m){ \
  163.      (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
  164.      (o) += (n); \
  165.    }
  166. #else
  167. #  define input(b,o,c,n,m){ \
  168.      REG1 char_type *p = &(b)[(o)>>3]; \
  169.      (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
  170.      ((long)(p[2])<<16))>>((o)&0x7))&(m); \
  171.      (o) += (n); \
  172.    }
  173. #endif
  174.  
  175. #ifndef MAXSEG_64K
  176.    /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
  177. #  define tab_prefixof(i) tab_prefix[i]
  178. #  define clear_tab_prefixof()    memzero(tab_prefix, 256);
  179. #else
  180.    /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
  181.    /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd  codes */
  182.    ush *tab_prefix[2];
  183. #  define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
  184. #  define clear_tab_prefixof()    \
  185.       memzero(tab_prefix0, 128), \
  186.       memzero(tab_prefix1, 128);
  187. #endif
  188. #define de_stack        ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
  189. #define tab_suffixof(i) tab_suffix[i]
  190.  
  191. int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  192.  
  193. /* ============================================================================
  194.  * Decompress in to out.  This routine adapts to the codes in the
  195.  * file building the "string" table on-the-fly; requiring no table to
  196.  * be stored in the compressed file.
  197.  * IN assertions: the buffer inbuf contains already the beginning of
  198.  *   the compressed data, from offsets iptr to insize-1 included.
  199.  *   The magic header has already been checked and skipped.
  200.  *   bytes_in and bytes_out have been initialized.
  201.  */
  202. int unlzw(in, out) 
  203.     int in, out;    /* input and output file descriptors */
  204. {
  205.     REG2   char_type  *stackp;
  206.     REG3   code_int   code;
  207.     REG4   int        finchar;
  208.     REG5   code_int   oldcode;
  209.     REG6   code_int   incode;
  210.     REG7   long       inbits;
  211.     REG8   long       posbits;
  212.     REG9   int        outpos;
  213. /*  REG10  int        insize; (global) */
  214.     REG11  unsigned   bitmask;
  215.     REG12  code_int   free_ent;
  216.     REG13  code_int   maxcode;
  217.     REG14  code_int   maxmaxcode;
  218.     REG15  int        n_bits;
  219.     REG16  long int        rsize;                 /* SPDsoft */
  220.     
  221. #ifdef MAXSEG_64K
  222.     tab_prefix[0] = tab_prefix0;
  223.     tab_prefix[1] = tab_prefix1;
  224. #endif
  225.     maxbits = get_byte();
  226.     block_mode = maxbits & BLOCK_MODE;
  227.     if ((maxbits & LZW_RESERVED) != 0) {
  228.     WARN((strerr, "%s: %s: warning, unknown flags 0x%x",
  229.           progname, ifname, maxbits & LZW_RESERVED));
  230.     }
  231.     maxbits &= BIT_MASK;
  232.     maxmaxcode = MAXCODE(maxbits);
  233.     
  234.     if (maxbits > BITS) {
  235.     sprintf(strerr,
  236.         "%s: compressed with %d bits, can only handle %d bits",
  237.         ifname, maxbits, BITS);
  238.     Calert(strerr);
  239.     exit_code = ERROR;
  240.     return ERROR;
  241.     }
  242.     rsize = insize;
  243.     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  244.     bitmask = (1<<n_bits)-1;
  245.     oldcode = -1;
  246.     finchar = 0;
  247.     outpos = 0;
  248.     posbits = inptr<<3;
  249.  
  250.     free_ent = ((block_mode) ? FIRST : 256);
  251.     
  252.     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
  253.     
  254.     for (code = 255 ; code >= 0 ; --code) {
  255.     tab_suffixof(code) = (char_type)code;
  256.     }
  257.     do {
  258.     REG1 int i;
  259.     int  e;
  260.     int  o;
  261.     
  262.     resetbuf:
  263.     
  264.       e = insize-(o = (posbits>>3));
  265.     
  266.     for (i = 0 ; i < e ; ++i) {
  267.         inbuf[i] = inbuf[i+o];
  268.     }
  269.     insize = e;
  270.     posbits = 0;
  271.     
  272.     if (insize < INBUF_EXTRA) {
  273.         if ((rsize = read(in, (char*)inbuf+insize, INBUFSIZ)) == EOF) {
  274.         read_error();
  275.         }
  276.         
  277. #ifdef _SPD_PROG_
  278.     SPDNow=(unsigned long int)tell(in);
  279.     if(SPDSystemTask()) raise(SIGINT);
  280. #endif 
  281.  
  282.         insize +=  rsize;
  283.         bytes_in += (ulg)rsize;
  284.     }
  285.     inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : 
  286.           ((long)insize<<3)-(n_bits-1));
  287.     
  288.     while (inbits > posbits) {
  289.         if (free_ent > maxcode) {
  290.         posbits = ((posbits-1) +
  291.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  292.         ++n_bits;
  293.         if (n_bits == maxbits) {
  294.             maxcode = maxmaxcode;
  295.         } else {
  296.             maxcode = MAXCODE(n_bits)-1;
  297.         }
  298.         bitmask = (1<<n_bits)-1;
  299.         goto resetbuf;
  300.         }
  301.         input(inbuf,posbits,code,n_bits,bitmask);
  302.         Tracev((stderr, "%d ", code));
  303.  
  304.         if (oldcode == -1) {
  305.         if (code >= 256) error("corrupt input.");
  306.         outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
  307.         continue;
  308.         }
  309.         if (code == CLEAR && block_mode) {
  310.         clear_tab_prefixof();
  311.         free_ent = FIRST - 1;
  312.         posbits = ((posbits-1) +
  313.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  314.         maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  315.         bitmask = (1<<n_bits)-1;
  316.         goto resetbuf;
  317.         }
  318.         incode = code;
  319.         stackp = de_stack;
  320.         
  321.         if (code >= free_ent) { /* Special case for KwKwK string. */
  322.         if (code > free_ent) {
  323. #ifdef DEBUG
  324.             char_type *p;
  325.  
  326.             posbits -= n_bits;
  327.             p = &inbuf[posbits>>3];
  328.             fprintf(stderr,
  329.                 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
  330.                 code, free_ent, n_bits, insize);
  331.             fprintf(stderr,
  332.                 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
  333.                 posbits, p[-1],p[0],p[1],p[2],p[3]);
  334. #endif
  335.             if (!test && outpos > 0) {
  336.             write_buf(out, (char*)outbuf, outpos);
  337.             bytes_out += (ulg)outpos;
  338.             }
  339.             error(to_stdout ? "corrupt input." :
  340.               "corrupt input. Use zcat to recover some data.");
  341.         }
  342.         *--stackp = (char_type)finchar;
  343.         code = oldcode;
  344.         }
  345.  
  346.         while ((cmp_code_int)code >= (cmp_code_int)256) {
  347.         /* Generate output characters in reverse order */
  348.         *--stackp = tab_suffixof(code);
  349.         code = tab_prefixof(code);
  350.         }
  351.         *--stackp =    (char_type)(finchar = tab_suffixof(code));
  352.         
  353.         /* And put them out in forward order */
  354.         {
  355.         REG1 int    i;
  356.         
  357.         if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
  358.             do {
  359.             if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
  360.  
  361.             if (i > 0) {
  362.                 memcpy(outbuf+outpos, stackp, i);
  363.                 outpos += i;
  364.             }
  365.             if (outpos >= OUTBUFSIZ) {
  366.                 if (!test) {
  367.                 write_buf(out, (char*)outbuf, outpos);
  368.                 bytes_out += (ulg)outpos;
  369.                 }
  370.                 outpos = 0;
  371.             }
  372.             stackp+= i;
  373.             } while ((i = (de_stack-stackp)) > 0);
  374.         } else {
  375.             memcpy(outbuf+outpos, stackp, i);
  376.             outpos += i;
  377.         }
  378.         }
  379.  
  380.         if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
  381.  
  382.         tab_prefixof(code) = (unsigned short)oldcode;
  383.         tab_suffixof(code) = (char_type)finchar;
  384.         free_ent = code+1;
  385.         } 
  386.         oldcode = incode;    /* Remember previous code.    */
  387.     }
  388.     } while (rsize != 0);
  389.     
  390.     if (!test && outpos > 0) {
  391.     write_buf(out, (char*)outbuf, outpos);
  392.     bytes_out += (ulg)outpos;
  393.     }
  394.     return OK;
  395. }
  396.